package cn.edu.neusoft.food729.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.retroiftdemo_729.R;
import com.squareup.picasso.Picasso;


import java.util.List;

import cn.edu.neusoft.food729.Bean.CollectBean;
import cn.edu.neusoft.food729.Bean.CommentsBean;
import cn.edu.neusoft.food729.adapter.FoodInfoAdapter;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.listener.CollectListener;
import cn.edu.neusoft.food729.listener.CommentsListener;
import cn.edu.neusoft.food729.model.CollectModel;
import cn.edu.neusoft.food729.model.CommentsModel;

public class FoodInfoActivity extends AppCompatActivity {

    private int flag = 1, food_id;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private FoodInfoAdapter myAdapter;
    private int page = 1;
    List<CommentsBean> list;

    private TextView name, price, recommend;
    private ImageButton collect;
    private ImageView foodimage;
    private Button buy;
    Context context;

    private ActionMenuItemView item;
    private boolean flagcollect = false;//收藏标志
    private CollectModel collectmodel;//收藏model
    private SharedPreferences sp;//简单存储
    private String sessionID = "";


    CollectListener collectListener = new CollectListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onResponse(String msg) {
            if (item == null)
                item = findViewById(R.id.menucollect);
            switch (msg) {
                case "collect":
//                    System.out.println("----收藏成功");
                    Toast.makeText(FoodInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    flagcollect = true;
                    item.setIcon(getDrawable(R.drawable.ic_shoucang_1));
                    break;
                case "collected":
//                    System.out.println("----已收藏");
                    Toast.makeText(FoodInfoActivity.this, "已收藏", Toast.LENGTH_SHORT).show();
                    flagcollect = true;
                    item.setIcon(getDrawable(R.drawable.ic_shoucang_1));
                    break;
                case "unCollect":
//                    System.out.println("----未收藏");
                    Toast.makeText(FoodInfoActivity.this, "未收藏", Toast.LENGTH_SHORT).show();
                    flagcollect = false;
                    item.setIcon(getDrawable(R.drawable.ic_shoucang));
                    break;
                case "collectDeleteFood":
//                    System.out.println("----取消收藏");
                    Toast.makeText(FoodInfoActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
                    flagcollect = false;
                    item.setIcon(getDrawable(R.drawable.ic_shoucang));
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    private CommentsListener listener = new CommentsListener() {
        @Override
        public void onResponse(List<CommentsBean> beanlist) {
            if (page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            myAdapter.setList(list);
            recyclerView.setAdapter(myAdapter);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(FoodInfoActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_info);
        context = FoodInfoActivity.this;

        init();


        CommentsModel model = new CommentsModel();
        food_id = getIntent().getIntExtra("food_id", -1);
        model.getResultList(food_id, listener);
        sp = context.getSharedPreferences("login", MODE_PRIVATE);
        readSP();
    }

    private void readSP() {
        sessionID = sp.getString("session", null);
    }

    private void init() {
        recyclerView = findViewById(R.id.recyclerView);

        layoutManager = new LinearLayoutManager(this);
        //准备数据
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        myAdapter = new FoodInfoAdapter(this);
        recyclerView.setAdapter(myAdapter);

        collect = findViewById(R.id.menucollect);
        name = findViewById(R.id.res_name);
        price = findViewById(R.id.food_price);
        recommend = findViewById(R.id.intro);
        foodimage = findViewById(R.id.food_image);
        buy = findViewById(R.id.buy);

        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FoodInfoActivity.this, BuyActivity.class);
                startActivity(intent);
            }
        });

        String image = getIntent().getStringExtra("image");
        Picasso.with(this)
                .load(Common.BASEURL + image)
                .into(foodimage);

        name.setText(getIntent().getStringExtra("food_name"));
        price.setText("价格:" + getIntent().getDoubleExtra("price", -1) + "元");
        recommend.setText("简介:" + getIntent().getStringExtra("intro"));
        setTitle("菜谱详情");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menuc, menu);
        collectmodel = new CollectModel();
        collectmodel.exists(sessionID, food_id, flag, collectListener);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menucollect:
                if (flagcollect) {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollectFood(sessionID, food_id, collectListener);
                } else {
                    System.out.println("----准备收藏");
                    collectmodel.collectFood(sessionID, food_id, collectListener);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
